//
//  HYWelcomeViewController.swift
//  WeiBo
//
//  Created by crazyfish on 16/3/14.
//  Copyright © 2016年 crazyfish. All rights reserved.
//

import UIKit
import SDWebImage
class HYWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        setupUI()
    }

    
    private func setupUI(){
        
        //添加子控件
        view.addSubview(headerView)
        view.addSubview(messageLabel)
        
        //给子控件添加约束
        
        headerView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.view)
            make.top.equalTo(200)
            make.size.equalTo(CGSizeMake(90, 90))
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(headerView)
            make.top.equalTo(headerView.snp_bottom).offset(16)
        }
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        //1.更新约束
        
        headerView.snp_updateConstraints { (make) -> Void in
            
            make.top.equalTo(100)
        }
        
        //2.执行动画
        /**
        
        
        - parameter <Tduration:
        - parameter delay:                  阻尼系数0->1
        - parameter usingSpringWithDamping:
        - parameter initialSpringVelocity:  弹簧的初始速度
        - parameter options:
        - parameter animations:
        - parameter completion:
        */
        
        
        UIView.animateWithDuration(1, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations:
            { () -> Void in
            //执行动画
            
                self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
            //动画执行完成的操作
                
              UIView.animateWithDuration(1, animations: { () -> Void in
              
                self.messageLabel.alpha = 1
                
              }, completion: { (_) -> Void in
            
                //执行动画完成跳转界面在这里操作
                
                //1.获取appdelegate里面的window
                
                let delegate = UIApplication.sharedApplication().delegate! as! AppDelegate
                
                let window = delegate.window
                
                //2设置跟控制器
                
                window?.rootViewController = HYMianViewController()
                
              })
        }
        
    }
    
    
    //懒加载控件
    private lazy var headerView: UIImageView = {
     
        let imageView = UIImageView()
        
        imageView.image = UIImage(named: "avatar_default_big")
        
        imageView.layer.cornerRadius = 45
        
        imageView.layer.masksToBounds = true
        
        let imageurlStr = HYUserAccountViewModel.sharedAccountViewModel.account?.avatar_large
        
        imageView.sd_setImageWithURL(NSURL(string: imageurlStr!), placeholderImage: UIImage(named: "avatar_default_big"))
        
        return imageView
    
    }()

    //懒加载消息label
    private lazy var messageLabel: UILabel = {
    
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 16)
        
        label.text = "欢迎回来"
        
        label.textAlignment = .Center
        
        label.alpha = 0
        
        label.sizeToFit()
        
        return label
    
    }()


}
